package com.warehouse.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.warehouse.dao.UserDao;
import com.warehouse.domain.User;
import com.warehouse.util.DBUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public User getObjectByUserName(String userName) {
		
		if (userName.isEmpty()) {
			System.out.println("用户名不能为空");
			return null;
		}
		
		User user = null;
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {
			conn = DBUtil.getConnection();
			pstmt = conn.prepareStatement("select * from user where userName = ? "); 
			pstmt.setString(1,userName);  //用户名
			rs = pstmt.executeQuery();
			
			while (rs.next()) {
				user = new User();
				user.setIds(rs.getString(1));
				user.setUserName(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setNickName(rs.getString(4));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.release(conn, pstmt, rs);
		}
		return user;
	}
}
